import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
// element
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
// axios
import axios from '@/api/api.js'
// promise
require('es6-promise').polyfill()

// 放入vue原型
Vue.prototype.$http = axios

Vue.config.productionTip = false

Vue.use(ElementUI)

// 路由的钩子函数 路由拦截
router.beforeEach((to, from, next) => {
  if (to.meta.requireAuth) {
    if (localStorage['token']) {
      next()
    } else {
      next({ path: '/login', query: { redirect: to.fullPath } })
    }
  } else {
    next()
  }

  /* 如果已登陆不允许直接跳转到 登录页面 */
  let fullPath = to.fullPath.substring(0, 6)
  if (fullPath === '/login') {
    if (localStorage['token']) {
      // 已经登录的，不能调到首页而是跳到from即将离开的页面
      next({
        path: from.fullPath
      })
    } else {
      next()
    }
  }
})

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')
